package com.ljl.rest;

import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author 刘家良
 * @date 2022/10/29 23:18
 * @description /
 */
@RestController
public class RabbitMqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = "fanout.queue1")
    public void receive1(Map<String, Object> msg) throws Exception {
        System.out.printf("收到消息：时间[%s],内容[%s]\r\n", msg.get("createTime"), msg.get("msg"));
    }

    @GetMapping("/sendMessage/{msg}")
    public String sendMessage(@PathVariable(name = "msg")String msg) {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", msg);
        map.put("createTime", LocalDateTime.now().toString());
        rabbitTemplate.convertAndSend("fanout.exchange", null, map);
        return "success";
    }

}
